package com.test0102;

public class LibraryManagement {
    public static void main(String[] args) {
                // 创建图书馆
        Library library = new Library("大连图书馆");

        // 创建书籍：书名、作者、借阅状态
        Book b1 = new Book("Java", "柏拉图", false);
        Book b2 = new Book("Mysql", "笛卡尔", false);
        Book b3 = new Book("AI", "亚里士多德", true);

        // 图书馆添加书籍
        library.add(b1);
        library.add(b2);
        library.add(b3);

        // 打印所有书籍信息
        library.printAll();

        // 找出特定作者的书籍
        Book bookByAuthor = library.findBookByAuthor("John Doe");

        System.out.println(bookByAuthor);

        // 找出借阅状态为借出的书籍
        Book[] borrowedBook = library.findBorrowedBook();
        System.out.println(borrowedBook);
    }
}
class Library{
    private String name;
    private Book[] bookArr = new Book[3];
    private int index=0;

    public Library() {
    }

    public Library(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Library{" +
                "name='" + name + '\'' +
                '}';
    }
    public void add(Book b){
        bookArr[index++] = b;
    }
    public void printAll(){
        for(int i =0;i<bookArr.length;i++){
            System.out.println(bookArr[i]);
        }
    }
    public Book findBookByAuthor(String s){
        for(Book a : bookArr){
            if(a.getAuthor().equals(s)){
                return a;

            }
        }
        return null;
    }
    public Book[] findBorrowedBook(){
        int count =0;
        int index1=0;
        for(int i =0;i<bookArr.length;i++){
            if(bookArr[i].isStatus()==true){
                count++;
            }
        }
        Book[] newArr = new Book[count];
        for(int i =0;i<bookArr.length;i++){
            if(bookArr[i].isStatus()==true){
              newArr[index1++]=bookArr[i];
            }
        }
        return newArr;
    }
}
class Book{
    private String book_name;
    private String author;
    private boolean status;

    public Book() {

    }

    public Book(String book_name, String author, boolean status) {
        this.book_name = book_name;
        this.author = author;
        this.status = status;
    }

    public String getBook_name() {
        return book_name;
    }

    public void setBook_name(String book_name) {
        this.book_name = book_name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return
                "书名:" + book_name + '\t' +
                "作者:" + author + '\t' +
                "状态:" + status ;

    }
}